package com.future.bantang.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.future.bantang.fragment.BaseFragment;
import com.lidroid.xutils.ViewUtils;

import java.util.List;

/**
 * Created by Administrator on 16-1-11.
 */
public class BaseActivity extends FragmentActivity {

    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        mFragmentManager=getSupportFragmentManager();
        init();
        loadDatas();
    }


    protected void init() {

    }

    protected void loadDatas() {

    }

    /**
     * 切换显示fragment的方法
     * @param containorRes
     * @param cls
     */
    protected void showFragment(int containorRes,Class<? extends BaseFragment> cls){
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        Fragment fragment = mFragmentManager.findFragmentByTag(cls.getName());
        if (fragment==null){
            fragment=BaseFragment.getInstance(cls);
            fragmentTransaction.add(containorRes,fragment,cls.getName());
        }
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (fragments!=null){
            for(Fragment f:fragments){
                if (!f.isHidden()){
                    fragmentTransaction.hide(f);
                    break;
                }
            }
        }
        fragmentTransaction.show(fragment).commit();
    }
}
